/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package b_and_b_properties;

import b_and_b_properties.data.DAO;
import b_and_b_properties.data.Property;
import b_and_b_properties.data.Unit;
import java.awt.Dimension;
import java.awt.event.WindowEvent;
import java.awt.event.WindowListener;
import javax.swing.JFrame;
import javax.swing.JScrollPane;
import javax.swing.JTabbedPane;
import javax.swing.event.ChangeEvent;
import javax.swing.event.ChangeListener;

/**
 *
 * @author 07105015
 */
public class RealEstateTabbed extends JFrame implements WindowListener {

    private JTabbedPane realEstate = new JTabbedPane(JTabbedPane.TOP, JTabbedPane.SCROLL_TAB_LAYOUT);
    private DAO dao;
    private Property prop;
    private PropertyEntryPanel pe;
    private PropertyDetailPanel pdp;
//    private PropertyFeaturesPanel pfp;
    private boolean isOldProperty = false;

    public RealEstateTabbed(DAO dao) {
        System.out.println("RET w/ dao");
        this.dao = dao;
        this.prop = new Property();
        this.pe = new PropertyEntryPanel(dao, prop);
        this.pdp = new PropertyDetailPanel(dao, prop);
        this.pe.setPDP(this.pdp);
        this.pdp.setPEP(pe);
        add(realEstate);
        JScrollPane spPE = new JScrollPane(pe);
        realEstate.add(spPE, "Property Entry");
        realEstate.add(pdp, "Property Details");
        this.setTitle("Real Estate Program");
        this.setPreferredSize(new Dimension(1095, 758));
        this.setVisible(true);
        this.pack();
        realEstate.addChangeListener(new ChangeListener() {

            public void stateChanged(ChangeEvent e) {
                pdp.newUnit(pe.getUnitSelected());
                if (pdp.isSaveNeeded()) {
                    pe.setUnit(pdp.saveUnit());
                    pe.setRooms(pdp.saveRooms());
                    pe.savePropertyData();
                    pdp.saveFunction();
                }

            }
        });
//        this.setDefaultCloseOperation(EXIT_ON_CLOSE);
    }

    /**
     * Overloaded constructor for search return functionality
     * @param dao is the data object the search returns
     * @param prop is the property object the search returns
     * @param unit is the unit object the search returns
     */
    public RealEstateTabbed(DAO dao, Property prop) {
        isOldProperty = true;
        this.dao = dao;
        this.prop = prop;
        this.pe = new PropertyEntryPanel(dao, prop);
        this.pdp = new PropertyDetailPanel(dao, prop, isOldProperty);
        add(realEstate);
        realEstate.add(pe, "Property Entry");
        realEstate.add(pdp, "Property Details");
        this.setTitle("Real Estate Program");
        this.setPreferredSize(new Dimension(1000, 700));
        this.setResizable(false);
        this.setVisible(true);
        this.pack();
        realEstate.addChangeListener(new ChangeListener() {

            public void stateChanged(ChangeEvent e) {
                pdp.newUnit(pe.getUnitSelected());
                if (pdp.isSaveNeeded()) {
                    pe.setUnit(pdp.saveUnit());
                    pe.setRooms(pdp.saveRooms());
                    pe.savePropertyData();
                    pdp.saveFunction();
                }
            }
        });
    }

    public void windowOpened(WindowEvent e) {
//        throw new UnsupportedOperationException("Not supported yet.");
    }

    public void windowClosing(WindowEvent e) {
        pdp.saveFunction();
        pe.savePropertyData();
    }

    public void windowClosed(WindowEvent e) {
        pdp.saveFunction();
        pe.savePropertyData();
        dao.shutdown();
//        throw new UnsupportedOperationException("Not supported yet.");
    }

    public void windowIconified(WindowEvent e) {
//        throw new UnsupportedOperationException("Not supported yet.");
    }

    public void windowDeiconified(WindowEvent e) {
//        throw new UnsupportedOperationException("Not supported yet.");
    }

    public void windowActivated(WindowEvent e) {
//        throw new UnsupportedOperationException("Not supported yet.");
    }

    public void windowDeactivated(WindowEvent e) {
//        throw new UnsupportedOperationException("Not supported yet.");
    }
}
